{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import tensorflow as tf\n",
    "from tensorflow.keras import layers,models,losses,regularizers,constraints\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"sequential\"\n",
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "dense (Dense)                (None, 64)                4160      \n",
      "_________________________________________________________________\n",
      "dense_1 (Dense)              (None, 10)                650       \n",
      "=================================================================\n",
      "Total params: 4,810\n",
      "Trainable params: 4,810\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "tf.keras.backend.clear_session()\n",
    "\n",
    "model = models.Sequential()\n",
    "model.add(layers.Dense(64,input_dim=64,\n",
    "                       kernel_regularizer=regularizers.l2(0.01),\n",
    "                       activity_regularizer=regularizers.l1(0.01),\n",
    "                       kernel_constraint=constraints.MaxNorm(max_value=2,axis=0))\n",
    "          )\n",
    "model.add(layers.Dense(10,\n",
    "                       kernel_regularizer=regularizers.l1_l2(0.01,0.01),activation='sigmoid'))\n",
    "model.compile(optimizer='rmsprop',\n",
    "              loss='sparse_categorical_crossentropy',metrics=['AUC'])\n",
    "model.summary()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "def focal_loss(gamma=2.,alpha=.25):\n",
    "    def focal_loss_fixed(y_true,y_pred):\n",
    "        pt_1 = tf.where(tf.equal(y_true,1),y_pred,tf.ones_like(y_pred))\n",
    "        pt_0 = tf.where(tf.equal(y_true,0),y_pred,tf.zeros_like(y_pred))\n",
    "        loss = -tf.sum(\n",
    "            alpha * tf.pow(1 - pt_1,gamma) * tf.log(1e-07 + pt_1)) \\\n",
    "        -tf.sum((1-alpha) * tf.pow(pt_0,gamma) * tf.log(1 -pt_0 + 1e-07))\n",
    "        return loss\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}